﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace TddBarForPhone
{
    class NunitTask
    {
        private readonly ProcessStartInfo _startInfo;

        public NunitTask(string nunitFullPath, string dllToTest, string arguments)
        {
            _startInfo = new ProcessStartInfo
            {
                FileName = nunitFullPath,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                Arguments = string.Format(" {0} \"{1}\"", arguments, dllToTest),
            };
        }

        public IEnumerable<string> RunTests()
        {
            var result = new List<String>();
            RunTests(result.Add);
            return result;
        }

        public void RunTests(Action<string> writeLine)
        {
            var process = new Process
            {
                StartInfo = _startInfo
            };
            process.Start();
            process.BeginOutputReadLine();
            process.OutputDataReceived += (s, evt) => writeLine(evt.Data);
            process.WaitForExit();
            process.Dispose();
        }
    }
}
